package com.yaozheng.vocationaltraining.service.impl;

import android.text.TextUtils;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IHundredQuestionsEncyclopediaView;
import com.yaozheng.vocationaltraining.service.HundredQuestionsEncyclopediaService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

@EBean
public class HundredQuestionsEncyclopediaServiceImpl implements
		HundredQuestionsEncyclopediaService {
	IHundredQuestionsEncyclopediaView iTokenBaseView;

	@Override
	public void init(IHundredQuestionsEncyclopediaView iTokenBaseView) {
		this.iTokenBaseView = iTokenBaseView;
	}

	@Background
	@Override
	public void getUserInfo() {
		try {
			OkHttpClientManager
					.getInstance()
					.get(Constants.ROOTURL + "/user/info",
							iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
								@Override
								public void responseSuccess(JSONObject jsonObject) {
									iTokenBaseView.userInfoSuccess(jsonObject);
								}

								@Override
								public void responseError(String error) {
									iTokenBaseView.userInfoError(error);
								}
							});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.userInfoError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

	@Background
	@Override
	public void list(int lastId, int cateId, String keyWord, String sortBy, String sort, int resolved, int status) {
		try {
			String url = "";
			if(resolved == -1){ //表示搜索的是待审核
				url = Constants.ROOTURL + "/knowledge/questions?id=required&cateId="
						+ cateId +"&keyword="+keyWord
						+ "&status="+status
						+"&sortBy="+sortBy
						+"&sort="+sort+"&offset="+lastId;
			}else{
				url = Constants.ROOTURL + "/knowledge/questions?cateId="
						+ cateId +"&keyword="+keyWord+ "&resolved="+resolved
						+ "&status="+status
						+ "&sortBy="+sortBy
						+"&sort="+sort+"&offset="+lastId;
			}

			OkHttpClientManager
					.getInstance()
					.get( url , iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
						@Override
						public void responseSuccess(JSONObject jsonObject) {
							iTokenBaseView.responseSuccess(jsonObject);
						}

						@Override
						public void responseError(String error) {
							iTokenBaseView.responseError(error);
						}
					});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

	@Background
	@Override
	public void loadCategories() {
		try {
			OkHttpClientManager
					.getInstance()
					.get(Constants.ROOTURL + "/knowledge/categories",
							iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
								@Override
								public void responseSuccess(JSONObject jsonObject) {
									iTokenBaseView.categoriesListSuccess(jsonObject);
								}

								@Override
								public void responseError(String error) {
									iTokenBaseView.categoriesListError(error);
								}
							});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.categoriesListError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

	@Override
	public void loadStatic() {
		try {
			OkHttpClientManager
					.getInstance()
					.get(Constants.ROOTURL + "/knowledge/statistics",
							iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
								@Override
								public void responseSuccess(JSONObject jsonObject) {
									iTokenBaseView.statisticsSuccess(jsonObject);
								}

								@Override
								public void responseError(String error) {

									iTokenBaseView.statisticsError(error);
								}
							});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.statisticsError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

}
